//
//  NewActivityViewController.m
//  haolaDoc
//
//  Created by chen on 2017/5/12.
//  Copyright © 2017年 deyi. All rights reserved.
//

#import "NewActivityViewController.h"
#import "AlarmPickView.h"
#import "AlarmTimePickView.h"
#import "AppMonitor.h"
@interface NewActivityViewController (){
    AlarmPickView *_alarmPickView;
    AlarmTimePickView *_alarmTimePickView;
}
//@property(nonatomic,strong) ActivityModel *activity;
@property (nonatomic, strong) UIDatePicker *DayPicker;
@property(nonatomic,strong) NSString *doctorId;
//@property(nonatomic,strong) NSNumber *startTime;
//@property(nonatomic,strong) NSNumber *endTime;
@property(nonatomic,strong) NSString *activityTitle;
@property(nonatomic,strong) NSString *place;
@property(nonatomic,strong) NSString *explain;
@property(nonatomic,strong) NSString *isAlarm;
@property(nonatomic,strong) NSString *alarmTime;
@property(nonatomic,strong) NSDate *start;
@property(nonatomic,strong) NSDate *end;
@property(nonatomic,assign) long startTime;
@property(nonatomic,assign) long endTime;
@end

@implementation NewActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _activityScrollView.contentSize=CGSizeMake(0,self.view.frame.size.height);
    [self addDataPicker:_startTimeField];
    [self addDataPicker:_endTimeField];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日HH时mm分"];
    _startTimeField.text=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
    _endTimeField.text=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate date]]];
    double timeStampDou =[[NSDate date] timeIntervalSince1970]*1000;
    _startTime=(long)timeStampDou;
    _endTime=(long)timeStampDou;
    _start=[NSDate date];
    _end=[NSDate date];
    
    _isAlarmField.text=@"不提醒";
    _alarmTimeField.text=@"10分钟前";
    // [_startTimeField addTarget:self action:@selector(inputStatusListener) forControlEvents:UIControlEventAllEvents];
    // [_endTimeField addTarget:self action:@selector(inputStatusListener) forControlEvents:UIControlEventAllEvents];
    [_isAlarmField addTarget:self action:@selector(inputStatusListener) forControlEvents:UIControlEventAllEvents];
    [_titleField addTarget:self action:@selector(inputStatusListener) forControlEvents:UIControlEventAllEvents];
    [_placeField addTarget:self action:@selector(inputStatusListener) forControlEvents:UIControlEventAllEvents];
    [_explainField addTarget:self action:@selector(inputStatusListener) forControlEvents:UIControlEventAllEvents];
    [_alarmSet setHidden:YES];
    [_alarmTimeField setHidden:YES];
    _createButton.alpha=0.4;
    [_createButton setEnabled:NO];
}
-(void)viewWillAppear:(BOOL)animated{
    _alarmPickView=[[AlarmPickView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-150, SCREEN_WIDTH, 150)];
    [_alarmPickView setShowsSelectionIndicator:YES];
    [_isAlarmField setInputView:_alarmPickView];
    _alarmPickView.outputView=_isAlarmField;
    
    _alarmTimePickView=[[AlarmTimePickView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-150, SCREEN_WIDTH, 150)];
    [_alarmTimePickView setShowsSelectionIndicator:YES];
    [_alarmTimeField setInputView:_alarmTimePickView];
    _alarmTimePickView.outputView=_alarmTimeField;
    
}
-(void)addDataPicker:(UITextField *)textField{
    self.DayPicker = [[UIDatePicker alloc]init];
    self.DayPicker.center=self.view.center;
    self.DayPicker.datePickerMode=UIDatePickerModeDateAndTime;
    self.DayPicker.locale=[NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日HH时mm分"];
    NSTimeInterval SevendayTime = 7* 24 * 60 * 60;
    NSDate *todayDate = [NSDate date];
    NSDate *SevendayFromToday = [todayDate dateByAddingTimeInterval:SevendayTime];
    self.DayPicker.minuteInterval = 15;
    //时间区间的最大值和最小值
    self.DayPicker.minimumDate = todayDate;
    
    self.DayPicker.maximumDate = SevendayFromToday;
    textField.inputView=self.DayPicker;
    if([textField isEqual:_startTimeField]){
        [self.DayPicker addTarget:self action:@selector(selectStartDate:)forControlEvents:UIControlEventValueChanged];
    }else if([textField isEqual:_endTimeField]){
        [self.DayPicker addTarget:self action:@selector(selectEndDate:)forControlEvents:UIControlEventValueChanged];
    }
    
    
}
-(void)selectStartDate:(UIDatePicker *)datePicker
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日HH时mm分"];
    _startTimeField.text = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:datePicker.date]];
    double timeInterval =[datePicker.date timeIntervalSince1970]*1000;
    _startTime=(long)timeInterval;
    _start=datePicker.date;
    
    
}
-(void)selectEndDate:(UIDatePicker *)datePicker
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日HH时mm分"];
    _endTimeField.text = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:datePicker.date]];
    double timeInterval =[datePicker.date timeIntervalSince1970]*1000;
    _endTime=(long)timeInterval;
    _end=datePicker.date;
}

-(void)writeActivity{
    NSString *title = [_titleField text];
    _activityTitle = [title stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    _place=_placeField.text;
    _place=[_place stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    _explain=_explainField.text;
    _explain = [_explain stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    if([_isAlarmField.text isEqualToString:@"不提醒"]){
        _isAlarm=@"0";
        _alarmTime=@"";
    }else{
        _isAlarm=@"1";
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
        NSDate *date;
        if([_alarmTimeField.text isEqualToString:@"10分钟前"]){
            date=[NSDate dateWithTimeInterval:-10*60 sinceDate:_start];
            
        }else if([_alarmTimeField.text isEqualToString:@"15分钟前"]){
            date=[NSDate dateWithTimeInterval:-15*60 sinceDate:_start];
        }else if([_alarmTimeField.text isEqualToString:@"30分钟前"]){
            date=[NSDate dateWithTimeInterval:-30*60 sinceDate:_start];
        }else if([_alarmTimeField.text isEqualToString:@"1小时前"]){
            date=[NSDate dateWithTimeInterval:-1*60*60 sinceDate:_start];
        }else if([_alarmTimeField.text isEqualToString:@"2小时前"]){
            date=[NSDate dateWithTimeInterval:-2*60*60 sinceDate:_start];
        }else{
            date=[NSDate dateWithTimeInterval:-24*60*60 sinceDate:_start];
        }
        _alarmTime=[dateFormatter stringFromDate:date];
        NSLog(@"%@",_alarmTime);
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)addActivityAction:(id)sender {
    if(_startTime<_endTime){
        if([AppMonitor checkIsLogin]){
            NSUserDefaults *userData=[NSUserDefaults standardUserDefaults];
            NSString *userID=[userData objectForKey:@"userId"];
            _doctorId=userID;
            [self writeActivity];
            NSString *url = [NSString stringWithFormat:@"/docSchedule/addSchedule?doctorId=%@&startTime=%ld&endTime=%ld&title=%@&place=%@&explain=%@&isAlarm=%@&alarmTime=%@",_doctorId,_startTime,_endTime,_activityTitle,_place,_explain,_isAlarm,_alarmTime];
            [AppMonitor parseDicByUrl:url withSuccessBlock:^(NSData *successDic) {
                NSDictionary *dataInfo=[NSJSONSerialization JSONObjectWithData:successDic options:0 error:nil];
                if([[dataInfo objectForKey:@"status"]isEqualToString:@"success"]){
                    [SVProgressHUD showSuccessWithStatus:@"添加成功"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [SVProgressHUD dismiss];
                    });
                    [[self navigationController] popViewControllerAnimated:YES];
                    
                }
            } andFailBlock:^(NSData *failDic) {
                
            }];
        }else{
            [AppMonitor jumpToLoginWithUIController:self.navigationController];
        }
        
        
    }else{
        
        [SVProgressHUD showWithStatus:@"请选择正确的起始时间！"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
    }

}

-(void)inputStatusListener{
    if([_isAlarmField.text isEqualToString:@"不提醒"]){
        [_alarmSet setHidden:YES];
        [_alarmTimeField setHidden:YES];
    }else{
        [_alarmTimeField setHidden:NO];
        [_alarmSet setHidden:NO];
    }
    if(!(_titleField.text.length>0&&_placeField.text.length>0&&_explainField.text.length>0)){
        _createButton.alpha=0.4;
        [_createButton setEnabled:NO];
    }else{
        _createButton.alpha=1;
        [_createButton setEnabled:YES];
    }
    
    
}

@end
